using GLib;
using TinyC;

public class Symbol : GLib.Object
{
    
    public string name { get; construct set; }
    public void *pointer { get; construct set; }
    
    public Symbol(string name, void *pointer)
    {
        Object(name : name, pointer : pointer);
    }
}
    
public class Backend : GLib.Object
{
    private Tcc tcc;
    
    public class Backend()
    {
        tcc = new Tcc();
        
        if(null != tcc)
        {
            tcc.set_output_type(OutPutType.MEMORY);
            
            tcc.add_sysinclude_path("include");
            
            tcc.add_sysinclude_path("include/auto_test");
            
            tcc.add_sysinclude_path("include/sec_api");
            
            tcc.add_sysinclude_path("include/sys");
            
            tcc.add_sysinclude_path("include/winapi");
        }
    }
    
    public static Symbol symbol(string name, void *pointer)
    {
        var symbol = new Symbol(name, pointer);

        return symbol;
    }
    
    public void import_symbol(Symbol symbol) {

        tcc.add_symbol(symbol.name, symbol.pointer);

        tcc.relocate();
    }
    
    public void import_symbols(Symbol[] symbols) {
        
        foreach(Symbol symbol in symbols)
        {
            tcc.add_symbol(symbol.name, symbol.pointer);
        }
        
        tcc.relocate();
    }
    
    public void *export_symbol(string name)
    {
        return tcc.get_symbol(name);
    }
    
    public bool compile_file (string filename)
    {
        if(-1 == tcc.add_file(filename))
	    {
	        return false;   
        }
	    
        return true;
    }
}
